<template>
<div class="about">
  <h2>x: {{x}}, y: {{y}}</h2>
  <hr>
  <h2 v-if="loading">请求中...</h2>
  <h2 v-else-if="errorMsg">{{errorMsg}}</h2>
  <!-- <img v-else :src="result.message" alt=""> -->
  <img v-else :src="result[0].url" alt="">
</div>
</template>

<script lang="ts">
import useClickPosition from '@/hooks/useClickPosition'
import useUrlLoader from '@/hooks/useUrlLoader'

import {watch} from 'vue'

interface ICat {
  id: string
  url: string
}

interface IDog {
  message: string
  status: string
}


export default {
  setup () {
    const {x, y} = useClickPosition()
    // const {loading, errorMsg, result} = useUrlLoader<IDog>('https://dog.ceo/api/breeds/image/random')
    const {loading, errorMsg, result} = useUrlLoader<ICat[]>('https://api.thecatapi.com/v1/images/search')

    watch(result, (val) => {
      if (val) {
        console.log(val[0].url)
      }
    })

    return {
      x,
      y,
      loading,
      errorMsg,
      result
    }
  }
}
</script>
